import Vue from "vue";
import Router from "vue-router";
import store from "../store/index";
import Login from "@/views/Login.vue";
// 商家页面
// import Main from "@/views/user/Main.vue";
const Main = () => import(/* webpackChunkName: "main_orders" */ "@/views/user/Main.vue");
const OrdersWaitAccept = () => import(/* webpackChunkName: "main_orders" */ "@/views/user/OrdersWaitAccept");
const OrdersWaitFinish = () => import(/* webpackChunkName: "main_orders" */ "@/views/user/OrdersWaitFinish");
const OrdersFinish = () => import(/* webpackChunkName: "main_orders" */ "@/views/user/OrdersFinish");
const OrdersCancel = () => import(/* webpackChunkName: "main_orders" */ "@/views/user/OrdersCancel");

const ProductEdit = () => import(/* webpackChunkName: "Product" */ "@/views/user/ProductEdit");
const ProductList = () => import(/* webpackChunkName: "Product" */ "@/views/user/ProductList");

const Profile = () => import(/* webpackChunkName: "Profile" */ "@/views/user/Profile");

// 管理员页面
// import MainAdmin from "@/views/admin/MainAdmin.vue";
const MainAdmin = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/MainAdmin");
const WaitCheckList = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/ProductWaitCheckList");
const Check = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/ProductCheck");
const PassCheckList = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/ProductPassCheckList");
const NoPassCheckList = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/ProductNoPassCheckList");
const Close = () => import(/* webpackChunkName: "mainAdmin_check" */ "@/views/admin/ProductClose");

const Users = () => import(/* webpackChunkName: "users_cate_shopOrders" */ "@/views/admin/Users.vue");
const Cate = () => import(/* webpackChunkName: "users_cate_shopOrders" */ "@/views/admin/Cate.vue");
const BgImage = () => import(/* webpackChunkName: "users_cate_shopOrders" */ "@/views/admin/BgImage.vue");
const TagManage = () => import(/* webpackChunkName: "users_cate_shopOrders" */ "@/views/admin/TagManage.vue");
const ShopOrders = () => import(/* webpackChunkName: "users_cate_shopOrders" */ "@/views/admin/ShopOrders.vue");

const ServiceAndCode = () => import(/* webpackChunkName: "serviceAndCode_sysInfo" */ "@/views/admin/ServiceAndCode.vue");
const SysInfo = () => import(/* webpackChunkName: "serviceAndCode_sysInfo" */ "@/views/admin/SysInfo.vue");
const AdminProfile = () => import(/* webpackChunkName: "serviceAndCode_sysInfo" */ "@/views/admin/AdminProfile.vue");

const Agency = () => import(/* webpackChunkName: "serviceAndCode_sysInfo" */ "@/views/admin/Agency.vue");

Vue.use(Router);

// 修改页面路由重复报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const router = new Router({
  routes: [
    { path: "/login", name: "login", component: Login, meta: { isPublic: true } },
    {
      path: "/",
      name: "main",
      component: Main,
      children: [
        { path: "/product/edit", component: ProductEdit, meta: { title: "商品管理" }, name: "新建商品" },
        { path: "/product/edit/:id", component: ProductEdit, props: true, meta: { title: "商品管理" }, name: "编辑商品" },
        { path: "/product/list", component: ProductList, meta: { title: "商品管理" }, name: "商品列表" },

        { path: "/orders/waitAccept", component: OrdersWaitAccept, meta: { title: "订单管理" }, name: "待受理" },
        { path: "/orders/waitFinish", component: OrdersWaitFinish, meta: { title: "订单管理" }, name: "待完成" },
        { path: "/orders/finish", component: OrdersFinish, meta: { title: "订单管理" }, name: "已完成" },
        { path: "/orders/cancel", component: OrdersCancel, meta: { title: "订单管理" }, name: "已取消" },

        { path: "profile", component: Profile, meta: { title: "系统设置" }, name: "账号信息" },
      ],
    },
    {
      path: "/MainAdmin",
      name: "MainAdmin",
      component: MainAdmin,
      children: [
        { path: "/productCheck/wait", component: WaitCheckList, meta: { title: "商品审核" }, name: "等待审核" },
        { path: "/productCheck/wait/:id", component: Check, props: true, name: "审核" },
        { path: "/productCheck/pass", component: PassCheckList, meta: { title: "商品审核" }, name: "通过审核" },
        { path: "/productCheck/noPass", component: NoPassCheckList, meta: { title: "商品审核" }, name: "未通过审核" },
        { path: "/productCheck/close", component: Close, meta: { title: "商品审核" }, name: "下架的商品" },

        { path: "/cate", component: Cate, meta: { title: "商品管理" }, name: "商品分类" },
        { path: "/bgImage", component: BgImage, meta: { title: "商品管理" }, name: "商品背景图" },
        { path: "/tagManage", component: TagManage, meta: { title: "商品管理" }, name: "下单方式和服务费" },

        { path: "/users", component: Users, meta: { title: "商家管理" }, name: "商品列表" },
        { path: "/shopOrders", component: ShopOrders, meta: { title: "商家管理" }, name: "商家订单" },

        // { path: "/pay/edit", component: PayEdit },
        // { path: "/pay/edit/:id", component: PayEdit, props: true },
        // { path: "/pay/list", component: PayList },

        { path: "/serviceAndCode", component: ServiceAndCode, meta: { title: "前台综合管理" }, name: "客服和邀请码" },
        { path: "/sysInfo", component: SysInfo, meta: { title: "前台综合管理" }, name: "客户端通知" },
        { path: "/adminProfile", component: AdminProfile, meta: { title: "前台综合管理" }, name: "账号信息" },

        { path: "/agency", component: Agency, meta: { title: "代理管理" }, name: "代理列表" },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.name !== "login" && !store.state.token) {
    next("/login");
  } else if (to.name !== "login" && store.state.code.includes("ERROR")) {
    router.app.$alert(store.state.msg, "", {
      confirmButtonText: "确定",
      callback: () => {
        store.commit("Logout");
        next("/login");
      },
    });
  }
  // else if (to.name !== "login" && store.state.code !== "0" && !store.state.code.includes("ERROR")) {
  // router.app.$alert("出现错误，请重新登录", "", {
  //   confirmButtonText: "确定",
  //   callback: () => {
  //     store.commit("Logout");
  //     next("/login");
  //   },
  // });
  // }
  else {
    next();
  }
});

export default router;
